//
//  APIHSBCWord.m
//  Bracelet
//
//  Created by jing zhao on 3/12/15.
//  Copyright (c) 2015 jing zhao. All rights reserved.
//

#import "APIHSBCWord.h"
#import "FileReadAndWrite.h"
#define FILENAMECACHE_APIHSBCWORLD @"fileName_apiHsbcWorld"

@implementation APIHSBCWord
-(instancetype)initWorldData:(NSDictionary*)dic
{
    self= [super init];
    if (self) {
        
        self.strHtml = [dic objectForKey:APIKEYHTML];
        //如果使用cities info 可以将array里的dic 作为对象拆分
        //        self.arrayWorldInfo = [dic objectForKey:APIKEYWORLDINFO];
        
    }
    return self;
}

+(instancetype)apiWorldData:(NSDictionary*)dic
{
    return [[self alloc]initWorldData:dic];
}

+(void)getHSBCWorldWithData:(NSDictionary *)dataDic Success:(void (^)( APIHSBCWord *world))successBlock
                       failure:(void (^)( NSError *error)) failureBlock
{
    NSString *strUserId = [[NSUserDefaults standardUserDefaults]objectForKey:USERID];
    NSDictionary *headerData =@{@"BasicAuthUsername":strUserId};
    
    [[APIConnection shareInstance]post:@"hbcn/hbcnWorld" data:dataDic header:headerData netError:^(NSError *error) {
        failureBlock(error);
        
        //取缓存
        NSDictionary *dicCanch = [FileReadAndWrite getTheStorageDataWithFile:FILENAMECACHE_APIHSBCWORLD];
        if (dicCanch) {
            NSDictionary *dicSuccess = [dicCanch objectForKey:APIKEYData];
            successBlock([APIHSBCWord apiWorldData:dicSuccess]);
        }
    } apiFinish:^(id data) {
        
        NSDictionary *dictionaryTemp = data;
        NSInteger codeTemp= [[dictionaryTemp objectForKey:APIKEYCODE]integerValue];
        if (codeTemp ==0) {
            NSDictionary *dicSuccess = [dictionaryTemp objectForKey:APIKEYData];
            successBlock([APIHSBCWord apiWorldData:dicSuccess]);
            
            //存入缓存
            [FileReadAndWrite storageDataWithFile:FILENAMECACHE_APIHSBCWORLD andData:dictionaryTemp];
            
        }
        else
        {
            failureBlock(nil);
        }
    }];
}
@end
